/*
 * Author: Rolando Fentanes
 * Class: Board
 * The board class represents a single board in the game.
 */

import java.awt.Dimension; // flexibility
import java.awt.Point; // flexibility
import java.util.LinkedList;

class Board {
    // first dimension is rows, second is columns
    public LinkedList<Piece>[][] Positions;
    // list of moable pieces for faster access by Rules
    public LinkedList<Piece> Pieces;
    public Dimension size;
    public int ID;

    Board(int id, Dimension size) {
        this.ID = id;
        this.size = size;
        // Initializes a multidimensional array of linked list to store
        // pieces in. Decided to use linked list because if a space on
        // the board is empty, the space in memory won't be
        // allocated for pieces.
        Positions = new LinkedList[this.size.width][this.size.height];
        for(int i = 0; i < this.size.width; i++)
            for(int k = 0; k < this.size.height; k++)
                Positions[i][k] = new LinkedList();

        Pieces = new LinkedList();
    }
    
    // returns a list of all pieces in one spot on the board
    public LinkedList<Piece> getPieces(Point point) {
	return Positions[point.x][point.y];
    }

    // adds piece to Positions
    public void addPiece(Piece piece, Point point) {
        // update piece location so it matches point and board
        piece.location.move(point.x, point.y, ID);
        // checks if pieces is moveable,
        // if it is it is added to the front of the list
        // this is intended to help with
        // the order the GUI draws pieces
        if(piece.moveRange != 0) {
            Positions[point.x][point.y].addFirst(piece);
            Pieces.add(piece);
        }
        else
            Positions[point.x][point.y].addLast(piece);
    }

    // removes piece from Positions
    public void removePiece(Piece piece, Point point) {
        Positions[point.x][point.y].remove(piece);
        if(piece.moveRange != 0)
            Pieces.remove(piece);
    }
}
